<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>动态组件</title>
  </head>
  <body>
    <script src="https://unpkg.com/vue@next"></script>
    <div id="root"></div>

    <script>
      // 动态组件 : 根据数据的变化  结合component标签  来随时动态切换组件的显示
      const app = Vue.createApp({
        data() {
          return {
            currentItem: "input-item",
          };
        },
        methods: {
          hanleClick() {
            if (this.currentItem === "input-item") {
              this.currentItem = "common-item";
            } else {
              this.currentItem = "input-item";
            }
          },
        },
        template: `
                <keep-alive>
                    <component :is="currentItem"/>
                </keep-alive>
                <button @click="hanleClick">切换</button>
                `,
      });

      app.component("input-item", {
        template: ` 
                <input>   
        `,
      });

      app.component("common-item", {
        template: ` 
              <div>hello world</div>
        `,
      });

      const vm = app.mount("#root");
    </script>
  </body>
</html>
